<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>jsloader.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_jsloader.js.html">jsloader.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility to load JavaScript files via DOM script tags.
<a name="line17"></a> * Refactored from goog.net.Jsonp. Works cross-domain.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.net.jsloader&#39;);
<a name="line22"></a>goog.provide(&#39;goog.net.jsloader.Error&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.array&#39;);
<a name="line25"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom&#39;);
<a name="line28"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * The name of the property of goog.global under which the JavaScript
<a name="line33"></a> * verification object is stored by the loaded script.
<a name="line34"></a> * @type {string}
<a name="line35"></a> * @private
<a name="line36"></a> */
<a name="line37"></a>goog.net.jsloader.GLOBAL_VERIFY_OBJS_ = &#39;closure_verification&#39;;
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>/**
<a name="line41"></a> * The default length of time, in milliseconds, we are prepared to wait for a
<a name="line42"></a> * load request to complete.
<a name="line43"></a> * @type {number}
<a name="line44"></a> */
<a name="line45"></a>goog.net.jsloader.DEFAULT_TIMEOUT = 5000;
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Optional parameters for goog.net.jsloader.send.
<a name="line50"></a> * timeout: The length of time, in milliseconds, we are prepared to wait
<a name="line51"></a> *     for a load request to complete. Default it 5 seconds.
<a name="line52"></a> * document: The HTML document under which to load the JavaScript. Default is
<a name="line53"></a> *     the current document.
<a name="line54"></a> * cleanupWhenDone: If true clean up the script tag after script completes to
<a name="line55"></a> *     load. This is important if you just want to read data from the JavaScript
<a name="line56"></a> *     and then throw it away. Default is false.
<a name="line57"></a> *
<a name="line58"></a> * @typedef {{
<a name="line59"></a> *   timeout: (number|undefined),
<a name="line60"></a> *   document: (HTMLDocument|undefined),
<a name="line61"></a> *   cleanupWhenDone: (boolean|undefined)
<a name="line62"></a> * }}
<a name="line63"></a> */
<a name="line64"></a>goog.net.jsloader.Options;
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Scripts (URIs) waiting to be loaded.
<a name="line69"></a> * @type {Array.&lt;string&gt;}
<a name="line70"></a> * @private
<a name="line71"></a> */
<a name="line72"></a>goog.net.jsloader.scriptsToLoad_ = [];
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Loads and evaluates the JavaScript files at the specified URIs, guaranteeing
<a name="line77"></a> * the order of script loads.
<a name="line78"></a> *
<a name="line79"></a> * Because we have to load the scripts in serial (load script 1, exec script 1,
<a name="line80"></a> * load script 2, exec script 2, and so on), this will be slower than doing
<a name="line81"></a> * the network fetches in parallel.
<a name="line82"></a> *
<a name="line83"></a> * If you need to load a large number of scripts but dependency order doesn&#39;t
<a name="line84"></a> * matter, you should just call goog.net.jsloader.load N times.
<a name="line85"></a> *
<a name="line86"></a> * If you need to load a large number of scripts on the same domain,
<a name="line87"></a> * you may want to use goog.module.ModuleLoader.
<a name="line88"></a> *
<a name="line89"></a> * @param {Array.&lt;string&gt;} uris The URIs to load.
<a name="line90"></a> * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
<a name="line91"></a> *     goog.net.jsloader.options documentation for details.
<a name="line92"></a> */
<a name="line93"></a>goog.net.jsloader.loadMany = function(uris, opt_options) {
<a name="line94"></a>  // Loading the scripts in serial introduces asynchronosity into the flow.
<a name="line95"></a>  // Therefore, there are race conditions where client A can kick off the load
<a name="line96"></a>  // sequence for client B, even though client A&#39;s scripts haven&#39;t all been
<a name="line97"></a>  // loaded yet.
<a name="line98"></a>  //
<a name="line99"></a>  // To work around this issue, all module loads share a queue.
<a name="line100"></a>  if (!uris.length) {
<a name="line101"></a>    return;
<a name="line102"></a>  }
<a name="line103"></a>
<a name="line104"></a>  var isAnotherModuleLoading = goog.net.jsloader.scriptsToLoad_.length;
<a name="line105"></a>  goog.array.extend(goog.net.jsloader.scriptsToLoad_, uris);
<a name="line106"></a>  if (isAnotherModuleLoading) {
<a name="line107"></a>    // jsloader is still loading some other scripts.
<a name="line108"></a>    // In order to prevent the race condition noted above, we just add
<a name="line109"></a>    // these URIs to the end of the scripts&#39; queue and return.
<a name="line110"></a>    return;
<a name="line111"></a>  }
<a name="line112"></a>
<a name="line113"></a>  uris = goog.net.jsloader.scriptsToLoad_;
<a name="line114"></a>  var popAndLoadNextScript = function() {
<a name="line115"></a>    var uri = uris.shift();
<a name="line116"></a>    var deferred = goog.net.jsloader.load(uri, opt_options);
<a name="line117"></a>    if (uris.length) {
<a name="line118"></a>      deferred.addBoth(popAndLoadNextScript);
<a name="line119"></a>    }
<a name="line120"></a>  };
<a name="line121"></a>  popAndLoadNextScript();
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Loads and evaluates a JavaScript file.
<a name="line127"></a> * When the script loads, a user callback is called.
<a name="line128"></a> * It is the client&#39;s responsibility to verify that the script ran successfully.
<a name="line129"></a> *
<a name="line130"></a> * @param {string} uri The URI of the JavaScript.
<a name="line131"></a> * @param {goog.net.jsloader.Options=} opt_options Optional parameters. See
<a name="line132"></a> *     goog.net.jsloader.Options documentation for details.
<a name="line133"></a> * @return {!goog.async.Deferred} The deferred result, that may be used to add
<a name="line134"></a> *     callbacks and/or cancel the transmission.
<a name="line135"></a> *     The error callback will be called with a single goog.net.jsloader.Error
<a name="line136"></a> *     parameter.
<a name="line137"></a> */
<a name="line138"></a>goog.net.jsloader.load = function(uri, opt_options) {
<a name="line139"></a>  var options = opt_options || {};
<a name="line140"></a>  var doc = options.document || document;
<a name="line141"></a>
<a name="line142"></a>  var script = goog.dom.createElement(goog.dom.TagName.SCRIPT);
<a name="line143"></a>  var request = {script_: script, timeout_: undefined};
<a name="line144"></a>  var deferred = new goog.async.Deferred(goog.net.jsloader.cancel_, request);
<a name="line145"></a>
<a name="line146"></a>  // Set a timeout.
<a name="line147"></a>  var timeout = null;
<a name="line148"></a>  var timeoutDuration = goog.isDefAndNotNull(options.timeout) ?
<a name="line149"></a>      options.timeout : goog.net.jsloader.DEFAULT_TIMEOUT;
<a name="line150"></a>  if (timeoutDuration &gt; 0) {
<a name="line151"></a>    timeout = window.setTimeout(function() {
<a name="line152"></a>      goog.net.jsloader.cleanup_(script, true);
<a name="line153"></a>      deferred.errback(new goog.net.jsloader.Error(
<a name="line154"></a>          goog.net.jsloader.ErrorCode.TIMEOUT,
<a name="line155"></a>          &#39;Timeout reached for loading script &#39; + uri));
<a name="line156"></a>    }, timeoutDuration);
<a name="line157"></a>    request.timeout_ = timeout;
<a name="line158"></a>  }
<a name="line159"></a>
<a name="line160"></a>  // Hang the user callback to be called when the script completes to load.
<a name="line161"></a>  // NOTE(user): This callback will be called in IE even upon error. In any
<a name="line162"></a>  // case it is the client&#39;s responsibility to verify that the script ran
<a name="line163"></a>  // successfully.
<a name="line164"></a>  script.onload = script.onreadystatechange = function() {
<a name="line165"></a>    if (!script.readyState || script.readyState == &#39;loaded&#39; ||
<a name="line166"></a>        script.readyState == &#39;complete&#39;) {
<a name="line167"></a>      var removeScriptNode = options.cleanupWhenDone || false;
<a name="line168"></a>      goog.net.jsloader.cleanup_(script, removeScriptNode, timeout);
<a name="line169"></a>      deferred.callback(null);
<a name="line170"></a>    }
<a name="line171"></a>  };
<a name="line172"></a>
<a name="line173"></a>  // Add an error callback.
<a name="line174"></a>  // NOTE(user): Not supported in IE.
<a name="line175"></a>  script.onerror = function() {
<a name="line176"></a>    goog.net.jsloader.cleanup_(script, true, timeout);
<a name="line177"></a>    deferred.errback(new goog.net.jsloader.Error(
<a name="line178"></a>        goog.net.jsloader.ErrorCode.LOAD_ERROR,
<a name="line179"></a>        &#39;Error while loading script &#39; + uri));
<a name="line180"></a>  };
<a name="line181"></a>
<a name="line182"></a>  // Add the script element to the document.
<a name="line183"></a>  goog.dom.setProperties(script, {
<a name="line184"></a>    &#39;type&#39;: &#39;text/javascript&#39;,
<a name="line185"></a>    &#39;charset&#39;: &#39;UTF-8&#39;,
<a name="line186"></a>    // NOTE(user): Safari never loads the script if we don&#39;t set
<a name="line187"></a>    // the src attribute before appending.
<a name="line188"></a>    &#39;src&#39;: uri
<a name="line189"></a>  });
<a name="line190"></a>  var scriptParent = goog.net.jsloader.getScriptParentElement_(doc);
<a name="line191"></a>  scriptParent.appendChild(script);
<a name="line192"></a>
<a name="line193"></a>  return deferred;
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Loads a JavaScript file and verifies it was evaluated successfully, using a
<a name="line199"></a> * verification object.
<a name="line200"></a> * The verification object is set by the loaded JavaScript at the end of the
<a name="line201"></a> * script.
<a name="line202"></a> * We verify this object was set and return its value in the success callback.
<a name="line203"></a> * If the object is not defined we trigger an error callback.
<a name="line204"></a> *
<a name="line205"></a> * @param {string} uri The URI of the JavaScript.
<a name="line206"></a> * @param {string} verificationObjName The name of the verification object that
<a name="line207"></a> *     the loaded script should set.
<a name="line208"></a> * @param {goog.net.jsloader.Options} options Optional parameters. See
<a name="line209"></a> *     goog.net.jsloader.Options documentation for details.
<a name="line210"></a> * @return {!goog.async.Deferred} The deferred result, that may be used to add
<a name="line211"></a> *     callbacks and/or cancel the transmission.
<a name="line212"></a> *     The success callback will be called with a single parameter containing
<a name="line213"></a> *     the value of the verification object.
<a name="line214"></a> *     The error callback will be called with a single goog.net.jsloader.Error
<a name="line215"></a> *     parameter.
<a name="line216"></a> */
<a name="line217"></a>goog.net.jsloader.loadAndVerify = function(uri, verificationObjName, options) {
<a name="line218"></a>  // Define the global objects variable.
<a name="line219"></a>  if (!goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_]) {
<a name="line220"></a>    goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_] = {};
<a name="line221"></a>  }
<a name="line222"></a>  var verifyObjs = goog.global[goog.net.jsloader.GLOBAL_VERIFY_OBJS_];
<a name="line223"></a>
<a name="line224"></a>  // Verify that the expected object does not exist yet.
<a name="line225"></a>  if (goog.isDef(verifyObjs[verificationObjName])) {
<a name="line226"></a>    // TODO(user): Error or reset variable?
<a name="line227"></a>    return goog.async.Deferred.fail(new goog.net.jsloader.Error(
<a name="line228"></a>        goog.net.jsloader.ErrorCode.VERIFY_OBJECT_ALREADY_EXISTS,
<a name="line229"></a>        &#39;Verification object &#39; + verificationObjName + &#39; already defined.&#39;));
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  // Send request to load the JavaScript.
<a name="line233"></a>  var sendDeferred = goog.net.jsloader.load(uri, options);
<a name="line234"></a>
<a name="line235"></a>  // Create a deferred object wrapping the send result.
<a name="line236"></a>  var deferred = new goog.async.Deferred(sendDeferred.cancel);
<a name="line237"></a>
<a name="line238"></a>  // Call user back with object that was set by the script.
<a name="line239"></a>  sendDeferred.addCallback(function() {
<a name="line240"></a>    var result = verifyObjs[verificationObjName];
<a name="line241"></a>    if (goog.isDef(result)) {
<a name="line242"></a>      deferred.callback(result);
<a name="line243"></a>      delete verifyObjs[verificationObjName];
<a name="line244"></a>    } else {
<a name="line245"></a>      // Error: script was not loaded properly.
<a name="line246"></a>      deferred.errback(new goog.net.jsloader.Error(
<a name="line247"></a>          goog.net.jsloader.ErrorCode.VERIFY_ERROR,
<a name="line248"></a>          &#39;Script &#39; + uri + &#39; loaded, but verification object &#39; +
<a name="line249"></a>          verificationObjName + &#39; was not defined.&#39;));
<a name="line250"></a>    }
<a name="line251"></a>  });
<a name="line252"></a>
<a name="line253"></a>  // Pass error to new deferred object.
<a name="line254"></a>  sendDeferred.addErrback(function(error) {
<a name="line255"></a>    if (goog.isDef(verifyObjs[verificationObjName])) {
<a name="line256"></a>      delete verifyObjs[verificationObjName];
<a name="line257"></a>    }
<a name="line258"></a>    deferred.errback(error);
<a name="line259"></a>  });
<a name="line260"></a>
<a name="line261"></a>  return deferred;
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Gets the DOM element under which we should add new script elements.
<a name="line267"></a> * How? Take the first head element, and if not found take doc.documentElement,
<a name="line268"></a> * which always exists.
<a name="line269"></a> *
<a name="line270"></a> * @param {!HTMLDocument} doc The relevant document.
<a name="line271"></a> * @return {!Element} The script parent element.
<a name="line272"></a> * @private
<a name="line273"></a> */
<a name="line274"></a>goog.net.jsloader.getScriptParentElement_ = function(doc) {
<a name="line275"></a>  var headElements = doc.getElementsByTagName(goog.dom.TagName.HEAD);
<a name="line276"></a>  if (!headElements || goog.array.isEmpty(headElements)) {
<a name="line277"></a>    return doc.documentElement;
<a name="line278"></a>  } else {
<a name="line279"></a>    return headElements[0];
<a name="line280"></a>  }
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a> * Cancels a given request.
<a name="line286"></a> * @this {{script_: Element, timeout_: number}} The request context.
<a name="line287"></a> * @private
<a name="line288"></a> */
<a name="line289"></a>goog.net.jsloader.cancel_ = function() {
<a name="line290"></a>  var request = this;
<a name="line291"></a>  if (request &amp;&amp; request.script_) {
<a name="line292"></a>    var scriptNode = request.script_;
<a name="line293"></a>    if (scriptNode &amp;&amp; scriptNode.tagName == &#39;SCRIPT&#39;) {
<a name="line294"></a>      goog.net.jsloader.cleanup_(scriptNode, true, request.timeout_);
<a name="line295"></a>    }
<a name="line296"></a>  }
<a name="line297"></a>};
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>/**
<a name="line301"></a> * Removes the script node and the timeout.
<a name="line302"></a> *
<a name="line303"></a> * @param {Node} scriptNode The node to be cleaned up.
<a name="line304"></a> * @param {boolean} removeScriptNode If true completely remove the script node.
<a name="line305"></a> * @param {?number=} opt_timeout The timeout handler to cleanup.
<a name="line306"></a> * @private
<a name="line307"></a> */
<a name="line308"></a>goog.net.jsloader.cleanup_ = function(scriptNode, removeScriptNode,
<a name="line309"></a>                                      opt_timeout) {
<a name="line310"></a>  if (goog.isDefAndNotNull(opt_timeout)) {
<a name="line311"></a>    goog.global.clearTimeout(opt_timeout);
<a name="line312"></a>  }
<a name="line313"></a>
<a name="line314"></a>  scriptNode.onload = goog.nullFunction;
<a name="line315"></a>  scriptNode.onerror = goog.nullFunction;
<a name="line316"></a>  scriptNode.onreadystatechange = goog.nullFunction;
<a name="line317"></a>
<a name="line318"></a>  // Do this after a delay (removing the script node of a running script can
<a name="line319"></a>  // confuse older IEs).
<a name="line320"></a>  if (removeScriptNode) {
<a name="line321"></a>    window.setTimeout(function() {
<a name="line322"></a>      goog.dom.removeNode(scriptNode);
<a name="line323"></a>    }, 0);
<a name="line324"></a>  }
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Possible error codes for jsloader.
<a name="line330"></a> * @enum {number}
<a name="line331"></a> */
<a name="line332"></a>goog.net.jsloader.ErrorCode = {
<a name="line333"></a>  LOAD_ERROR: 0,
<a name="line334"></a>  TIMEOUT: 1,
<a name="line335"></a>  VERIFY_ERROR: 2,
<a name="line336"></a>  VERIFY_OBJECT_ALREADY_EXISTS: 3
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>
<a name="line341"></a>/**
<a name="line342"></a> * A jsloader error.
<a name="line343"></a> *
<a name="line344"></a> * @param {goog.net.jsloader.ErrorCode} code The error code.
<a name="line345"></a> * @param {string=} opt_message Additional message.
<a name="line346"></a> * @constructor
<a name="line347"></a> * @extends {goog.debug.Error}
<a name="line348"></a> */
<a name="line349"></a>goog.net.jsloader.Error = function(code, opt_message) {
<a name="line350"></a>  var msg = &#39;Jsloader error (code #&#39; + code + &#39;)&#39;;
<a name="line351"></a>  if (opt_message) {
<a name="line352"></a>    msg += &#39;: &#39; + opt_message;
<a name="line353"></a>  }
<a name="line354"></a>  goog.base(this, msg);
<a name="line355"></a>
<a name="line356"></a>  /**
<a name="line357"></a>   * The code for this error.
<a name="line358"></a>   *
<a name="line359"></a>   * @type {goog.net.jsloader.ErrorCode}
<a name="line360"></a>   */
<a name="line361"></a>  this.code = code;
<a name="line362"></a>};
<a name="line363"></a>goog.inherits(goog.net.jsloader.Error, goog.debug.Error);
</pre>


</body>
</html>
